layui.use(['table', 'form', 'jquery', 'laydate', 'layer'], function () {
    let table = layui.table;
    let form = layui.form;
    let laydate = layui.laydate;
    let layer = layui.layer;
    let $ = layui.jquery;

    let BIZ_NAME = 'cost';
    let BIZ_TABLE = 'cost-table';
    let BIZ_FORM = 'cost-form';
    let BIZ_QUERY = 'cost-query';
    let tableCostObj;
    let formCostObj;

    let cols = [
        [
            {type: 'checkbox'},
            {title: '编号', field: 'id', align: 'center'},
            {title: '成本年月', field: 'yearmonth', align: 'center'},
            {title: '成本项目', field: 'name', align: 'center'},
            {title: '成本价', field: 'costPrice', align: 'center'},
            {title: '操作人姓名', field: 'operators', align: 'center'},
            {title: '标签', field: 'label', align: 'center'},
            {title: '创建时间', field: 'time', align: 'center'},
            {title: '操作', toolbar: '#cost-operate-bar', align: 'center', width: 130}
        ]
    ]
    window.renderCostTable = function (where) {
        tableCostObj = table.render({
            elem: '#' + BIZ_TABLE,
            url: '/' + BIZ_NAME + '/page',
            where: where,
            //data: [{id:1}], // 测试数据
            text: {none: '暂无相关数据'},
            response: {
                statusCode: 200,
                msgName: 'message'
            },
            page: true,
            limit: 10,
            limits: [10, 20, 50],
            cols: cols,
            skin: 'line',
            toolbar: '#cost-toolbar',
            defaultToolbar: [{
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        table.on('tool(' + BIZ_TABLE + ')', function (obj) {
            if (obj.event === 'remove') {
                window.removeCost(obj);
            } else if (obj.event === 'edit') {
                window.costForm(obj);
            }
        });

        table.on('toolbar(' + BIZ_TABLE + ')', function (obj) {
            if (obj.event === 'add') {
                window.costForm();
            } else if (obj.event === 'refresh') {
                window.refreshCost();
            }
        });
    }

    window.costForm = function (obj) {
        let title;
        if (obj) {
            title = '修改';
        }else {
            title = '新增';
        }
        formCostObj = layer.open({
            type: 1,
            title: title,
            shade: 0.1,
            area: ['22%', '60%'],
            btn: ['立即提交', '取消'],
            yes: function (index, layero) {
                $('#' + BIZ_FORM + ' [lay-submit]').click();
            },
            cancel: function (index, layero) {
                layer.close(index);
            },
            content: $('#' + BIZ_FORM),
            success: function (layero, index) {
                if(obj){
                    // 修改时填充数据
                    form.val(BIZ_FORM, obj.data);
                }else{
                    // 新增时清空表单
                    form.val(BIZ_FORM, {});
                    $('#costFormId').val('');
                    $('#costFormReset').click();
                }
            }
        });
    }

    window.removeCost = function (obj) {
        layer.confirm('确定要删除', {icon: 3, title: '提示'}, function (index) {
            layer.close(index);
            let loading = layer.load(2, {time: 5*1000});
            $.ajax({
                url: "/" + BIZ_NAME + "/remove/" + obj.data['id'],
                type: 'DELETE',
                success: function (result) {
                    layer.close(loading);
                    if (result.code === 200) {
                        layer.msg(result.message, {icon: 1, time: 1000}, function () {
                            obj.del();
                        });
                    } else {
                        layer.alert(result.message, {icon: 2});
                    }
                }
            })
        });
    }

    window.refreshCost = function (param) {
        tableCostObj.reload();
    }

    laydate.render({
        elem: '#costStart'
        , type: 'datetime'
    });

    laydate.render({
        elem: '#costEnd'
        , type: 'datetime'
    });

    //年月选择器
    laydate.render({
        elem: '#yearmonth'
        ,type: 'month'
    });

    form.on('submit(' + BIZ_QUERY + ')', function (data) {
        tableCostObj.reload({where: data.field})
        return false;
    });

    form.on('submit(' + BIZ_FORM + ')', function (data) {

        let url = '/save';
        let type = 'POST';
        //如果id不为空则调用修改接口
        if (data.field.id) {
            url = '/update'
        }

        $.ajax({
            url: '/' + BIZ_NAME + url,
            data: JSON.stringify(data.field),
            type: type,
            dataType: 'json',
            contentType: 'application/json',
            success: function (result) {
                if (result.code === 200) {
                    layer.msg(result.message, {icon: 1, time: 1000}, function () {
                        tableCostObj.reload();
                        layer.close(formCostObj);
                    });
                } else {
                    layer.alert(result.message, {icon: 2});
                }
            }
        })
        return false;
    });

    // 初始化运行区
    window.renderCostTable();
});